package com.hotel.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hotel.dao.RoleMapper;
import com.hotel.dao.UserRoleMapper;
import com.hotel.entity.Role;
import com.hotel.entity.User;
import com.hotel.entity.UserRole;
import com.hotel.service.RoleService;

@Service("roleService")
@Transactional(transactionManager="txManager")
public class RoleServiceImpl implements RoleService {
	@Resource
	private RoleMapper roleMapper;
	@Resource
	private UserRoleMapper userroleMapper;
	
	@Override
	public List<Role> findRoleByUserId(Integer userId,String choose,Integer pageNo,Integer pageSize) {
		//全部角色
		List<Role> allRole = roleMapper.findRoleByUserId(null,(pageNo-1)*pageSize,pageSize);
		//用户拥有的角色
		List<Role> roleList = roleMapper.findRoleByUserId(userId,(pageNo-1)*pageSize,pageSize);
		//当有条件时
		if(choose!=null){
			//授予角色,查出用户没有的角色
			if(choose.equals("add")){
				//遍历用户拥有的角色
				for (int i = 0; i < roleList.size(); i++) {
					Integer userRoleId = roleList.get(i).getRoleId();
					//将用户拥有的与全部角色比较
					for (int j = 0; j < allRole.size(); j++) {
						Integer roleId = allRole.get(j).getRoleId();
						if(userRoleId == roleId){
							allRole.remove(j);
							j--;
						}
					}
				}
			roleList = allRole;
			//撤销角色
			}else if(choose.equals("revoke")){
				//直接返回用户已有角色
			}
		}else{
			roleList = allRole;
		}
		return roleList;
	}

	@Override
	public int deleteByPrimaryKey(Integer id) {
		return roleMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(Role record) {
		return roleMapper.insert(record);
	}

	@Override
	public Role selectByPrimaryKey(Integer id) {
		return roleMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKey(Role record) {
		return roleMapper.updateByPrimaryKey(record);
	}
	/**
	 * 查询全部用户及对应的角色
	 */
	@Override
	public List<UserRole> findRolesByUser(Integer pageNo,Integer pageSize,List<UserRole> newUserRoles) {
		List<UserRole> userRoles = userroleMapper.findRolesByUser((pageNo-1)*pageSize,pageSize);
		if(newUserRoles==null){
			newUserRoles = new ArrayList<UserRole>();
		}
		Role newRole = null;
		//遍历全部用户
		for (int i = 0; i < userRoles.size(); i++) {
			//拿出用户i
			User user = userRoles.get(i).getUser();
			//拿出角色i
			newRole = userRoles.get(i).getRole();
			//拿出用户角色关系表id
			Integer id = userRoles.get(i).getUserroleId();
			
			//从1开始，第一个员工不与自己比较,最后一个员工不与自己比较
			for (int j = i+1; j < userRoles.size(); j++) {
				//newRole = userRoles.get(i).getRole();	
				String userName=userRoles.get(j).getUser().getName();
				//将用户i与其它用户比较  如果有用户名相同
				if(user.getName().equals(userName)){
					//用户i的另一个角色
					Role nextRole = userRoles.get(j).getRole();
					//同一用户的角色放到一起
					newRole.setRoleName(newRole.getRoleName()+","+nextRole.getRoleName());
					//清除掉已经合并的用户的另一个角色
					userRoles.remove(j);
					--j;
				}
			}
			//将用户角色重新组装
			UserRole userRole = new UserRole();
			userRole.setUserroleId(id);
			userRole.setUser(user);
			userRole.setRole(newRole);
			//添加到新集合
			newUserRoles.add(userRole);
			//当满足10条时结束
			if(newUserRoles.size()==10){
				return newUserRoles;
			}
			
			//如果遍历结束有合并,集合的数量就会小于10,重新调用自身,获取下10条
			if(i==userRoles.size()-1){
				if(newUserRoles.size()<10){
					findRolesByUser(pageNo+1,pageSize,newUserRoles);
				}
			}
		}
		return newUserRoles;
	}

	@Override
	public int insertUserRole(Integer userId,Integer roleId) {
		return userroleMapper.insertSelective(userId,roleId);
	}

	@Override
	public UserRole findByRoleId(Integer roleId) {
		return userroleMapper.findByRoleId(roleId);
	}

	@Override
	public UserRole findByUserRole(Integer userId, Integer roleId) {
		return userroleMapper.findByUserRole(userId, roleId);
	}

	@Override
	public int deleteByUserRoleId(Integer userRoleId) {
		return userroleMapper.deleteByPrimaryKey(userRoleId);
	}
}
